import "reflect-metadata";
import { createConnection } from "typeorm";
import { Maid } from "../entity/Maid";
import { Request, Response } from "express";
import { maids } from "../configs/configs";

const moment = require('moment-timezone');
const connR = createConnection("captain");

export function maidRoute(app: any) {

    // app.get('/api/maid/:maid_id', function (req: Request, res: Response) {

    //     connR.then(async connection => {

    //         let data = await connection.getRepository(Maid).findOne(req.params.maid_id);
    //         res.send(data)

    //     }).catch(error => console.log(error));
    // })


    app.get('/maids/:role_id', function (req: Request, res: Response) {

        connR.then(async connection => {

            let where = { role_id: req.params.role_id };
            let data = await connection.getRepository(Maid).find({ where: where, order: { is_friend: 'DESC', maid_id: 'ASC' } });
            data.forEach(element => {
                element.name = maids[element.maid_id].Name;
                element.skill_level = `${element.skill1_level}|${element.skill2_level}|${element.skill3_level}|${element.skill4_level}|${element.skill5_level}|${element.skill6_level}|${element.skill7_level}`;
            });

            // let data = await connection.getRepository(Maid).createQueryBuilder().select("COUNT(*) AS c").where().getRawOne();
            res.render('maid/index', { data: JSON.stringify(data) })

        }).catch(error => console.log(error));

    })
}
